package game.games.map2d;

import game.Abstract2DMap;
import game.AbstractGame;
import game.Position;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseMotionListener;

public class Map2D extends Abstract2DMap
{
    private static Map2D instance;
    
    public synchronized static Map2D getInstance()
    {
        if (instance == null)
        {
            synchronized (Map2D.class)
            {
                instance = new Map2D();
                // for (IConsoleCommand commandObj :
                // Map2DCommands.getCommands())
                // {
                // Console.getInstance().addConsoleCommand(commandObj);
                // }
            }
        }
        return instance;
    }

    private Position maxSize;
    private Position frameSize;
    private int mouseOverIndex = -1;
    
    @Override
    public void render(Graphics g)
    {
        if (frameSize != null)
        {
            g.setColor(Color.WHITE);
            g.fillRect(0, 0, frameSize.getWidth(), frameSize.getHeight());
            g.setColor(Color.BLACK);
            for (int index = 0; index < maxSize.getSum(); index++)
            {
                Position position = calculatePosition(index, maxSize);
                if (index == mouseOverIndex)
                {
                    Color originalColor = g.getColor();
                    g.setColor(Color.RED);
                    g.fillRect((position.getX() * cellSize) + 1, (position.getY() * cellSize) + 1, cellSize, cellSize);
                    g.setColor(originalColor);
                }
                else
                {
                    g.drawRect((position.getX() * cellSize) + 1, (position.getY() * cellSize) + 1, cellSize, cellSize);
                }
            }
        }
    }

    @Override
    protected void doNext()
    {
    }
    
    @Override
    protected synchronized void reset()
    {
        frameSize = new Position(Map2DRunner.getInstance().getSize());
        maxSize = calcMaxSize(frameSize);
        super.reset();
    }
    
    @Override
    public MouseListener getMouseControl()
    {
        return new MouseAdapter() {};
    }
    
    @Override
    public MouseMotionListener getMouseMotionListener()
    { 
        return new MouseMotionAdapter() 
        {

            @Override
            public void mouseMoved(MouseEvent e)
            {
                if (isRunning())
                {
                    try
                    {
                        mouseOverIndex = calculateIndex(calcCoord(e.getPoint()), maxSize); 
                    }
                    catch (IndexOutOfBoundsException ex)
                    {
                        //
                    }
                }
            }
            
            @Override
            public void mouseDragged(MouseEvent e)
            {
                if (isRunning())
                {
                    try
                    {
                        mouseOverIndex = calculateIndex(calcCoord(e.getPoint()), maxSize); 
                    }
                    catch (IndexOutOfBoundsException ex)
                    {
                        //
                    }
                }
            }
        };
    }
    
    public static class Map2DRunner extends AbstractGame
    {
        private static Map2DRunner instance;
        
        public synchronized static Map2DRunner getInstance()
        {
            synchronized (Map2DRunner.class)
            {
                if (instance == null)
                {
                    instance = new Map2DRunner();
                }
                return instance;
            }
        }
        
        protected Map2DRunner()
        {
            super(Map2D.getInstance());
        }
        
        public static void main(String[] args)
        {
            Thread t = new Thread(Map2DRunner.getInstance(), "Map2D");
            t.start();
        }
    }
}
